package com.zxy.leetcode.common;

import java.util.ArrayList;
import java.util.List;

public class ListNodeHelper {

    public static ListNode build(int count) {
        ListNode head = new ListNode(0);
        ListNode tail = head;
        for (int i=1; i<count; i++) {
            ListNode node = new ListNode(i);
            tail.next = node;
            tail = node;
        }
        return head;
    }

    public static ListNode build(int[] nums) {
        ListNode head = null;
        ListNode tail = null;
        for (int num : nums) {
            ListNode node = new ListNode(num);
            if (head == null) {
                head = node;
                tail = node;
            }else {
                tail.next = node;
                tail = node;
            }
        }

        return head;
    }

    public static void print(ListNode head) {
        if (head == null) {
            System.out.println("head is null");
            return;
        }

        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        System.out.println(list);
    }
}
